package com.leyou.item.controller;

import com.leyou.dto.PageDTO;
import com.leyou.item.dto.*;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.pojo.vo.SpuVo;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.acl.LastOwnerException;
import java.util.List;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/7 11:37
 * @description：
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpuService spuService;

    /**
     * 分页查询spu
     *
     * @param
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> findPage( @RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                             @RequestParam(value = "saleable", required = false) Boolean saleable,
                                             @RequestParam(value = "brandId", required = false) Long brandId,
                                             @RequestParam(value = "categoryId", required = false) Long categoryId,
                                             @RequestParam(value = "id", required = false) Long id) {
        return ResponseEntity.ok(spuService.findPage(page,rows,saleable,brandId,categoryId,id));
    }

    /**
     * 新增商品
     *
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity addGoods(@RequestBody SpuDTO spuDTO) {
        spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品
     *
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(200).build();
    }

    /**
     * 根据id批量查询sku
     *
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(skuService.querySkuByIds(ids));
    }

    /**
     * 根据spuID查询sku的集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(skuService.querySkuBySpuId(id));
    }

    /**
     * 根据id查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(spuDetailService.querySpuDetailBySpuId(id));
    }

    /**
     * 根据商品id（spuId）查询对应的规格参数组集合以及组内参数的信息，及对应值
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpec(
            @RequestParam("id")Long spuId,
            @RequestParam(value = "searching",required = false)Boolean searching){

        return ResponseEntity.ok(this.spuService.listSpec(spuId,searching));
    }

    /**
     * 修改商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity goodsOnAndOffShelves(@RequestParam(value = "id",required = false) Long id, @RequestParam(value = "saleable",required = false) Boolean saleable) {
        spuService.goodsOnAndOffShelves(id, saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> querySkuAndSpuAndApuDetailById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.querySkuAndSpuAndApuDetailById(id));
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }

    /**
     * 批量减库存
     * @param id
     * @return
     */
    /*@PutMapping("/stock/minus")
    public ResponseEntity inventoryReduction(@RequestBody Long id) {
        skuService.inventoryReduction(id);
        return ResponseEntity.status(200).build();
    }
*/
}
